const express = require('express');
const router = express.Router();
const link = require('../db/db');
router.get('/', (req, res) => {
    // res.render('admin/login', { msg: '' });
    //判断是否登录，如果登录进入主页
    if (req.cookies.email) {
        res.render('admin/user', {
            username: req.cookies.email
        });
    } else {
        //否则重定向到/login
        res.redirect('/admin/login');
    }
})

router.get('/login', (req, res) => {
    res.render('admin/login', {
        msg: ''
    });
})

router.post('/login', (req, res) => {
    // console.log(req.body);
    const email = req.body.email; //获取邮箱
    const pwd = req.body.password; //获取密码
    if (email.length == 0 || pwd.length == 0) {
        res.render('admin/login', {
            msg: "邮箱和密码不能为空"
        })
    } else {
        link.query("select * from user where email=?", [email], (err, data) => {
            if (err) {
                res.send(err);
            } else {
                // console.log(body);
                if (data.length == 0) {
                    res.render('admin/login', {
                        msg: "用户不存在"
                    });
                } else {
                    if (data[0].password == pwd) {
                        res.cookie('email', data[0].username);
                        // res.render('admin/user');
                        res.redirect('/admin/')
                    } else {
                        res.render('admin/login', {
                            msg: "密码错误"
                        });
                    }
                }
            }
        })
    }
})


router.get('/article', (req, res) => {
    // console.log(req.cookies.email);
    var username = req.cookies.email;
    link.query("select * from user where username=?", username, (err, data) => {
        if (err) {
            res.send("查询失败");
            return;
        }
        res.render('admin/article', {
            username: username
        });
    })
})

router.get('/article-edit', (req, res) => {
    res.render('admin/article-edit');
})

router.get('/user', (req, res) => {
    // console.log(req.cookies.email);
    var username = req.cookies.email;
    link.query("select * from user where username=?", username, (err, data) => {
        if (err) {
            res.send("查询失败");
            return;
        }
        res.render('admin/user', {
            // data: data[0],
            username: username
        });
    })
})
router.get('/user-edit', (req, res) => {
    // console.log(req.cookies.email);
    var username = req.cookies.email;
    link.query("select * from user where username=?", username, (err, data) => {
        if (err) {
            res.send("查询失败");
            return;
        }
        res.render('admin/user-edit', {
            data: data[0],
            username: username
        });
    })
})
router.post('/user-edit', (req, res) => {
        var body = req.body;
        // console.log(req.body);
        // console.log("insert into user(username,password,email,role,condition) values(?,?,?,?)");
        link.query("INSERT INTO `user` (`email`,`password`,`username`,`role`,`condition`) VALUES(?,?,?,?,?)", [body.email, body.password, body.username, body.role, body.condition], (err, data) => {
            if (err) {
                res.send("添加失败");
                return;
            }
            // res.render('admin/user');
            res.redirect('/admin/user');
        })
    })
    //退出功能
router.get('/layout', (req, res) => {
    //删除cookie
    res.clearCookie('email');
    //重定向到登录页面
    res.redirect('/admin/login');
})
module.exports = router;